<?php
/**
 * Urban Meandering functions and definitions
 *
 * @package WordPress
 * @subpackage urbanmeandering
 * @since urbanmeandering 1.0
 */

/**
 * Set the content width based on the theme's design and stylesheet.
 */
if ( ! isset( $content_width ) )
	$content_width = 584;



/**
 * @uses load_theme_textdomain() For translation/localization support.
 * @uses add_editor_style() To style the visual editor.
 * @uses add_theme_support() To add support for post thumbnails, automatic feed links, custom headers
 * 	and backgrounds, and post formats.
 * @uses register_nav_menus() To add support for navigation menus.
 * @uses register_default_headers() To register the default custom header images provided with the theme.
 * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
 *
 * @since urbanmeandering 1.0
 */


require_once( 'lib/um-options.php' );
function um_setup() {
	define( 'IMG', get_bloginfo( 'stylesheet_directory' ) . '/images/' );
  define('JSDIR', get_bloginfo('stylesheet_directory') . '/js/');
  define('CSSDIR', get_bloginfo('stylesheet_directory') . '/css/');
  define('CONTACT_FORM','51');
	
	add_editor_style();
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image' ) );
	add_theme_support( 'post-thumbnails' );
 
  add_image_size('85x70',85,70,true);
	add_image_size('302x170',302,170,true);
  add_image_size('585x250',585,250,true);
	add_image_size('1000x155',1000,155,true);
	
	register_nav_menu( 'primary', __( 'Primary Menu', 'urbanmeandering' ) );
}
add_action( 'after_setup_theme', 'um_setup' );

function um_title_tag(){

	global $page, $paged;
	wp_title( '|', true, 'right' );

	bloginfo( 'name' );

	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		echo " | $site_description";

	if ( $paged >= 2 || $page >= 2 )
		echo ' | ' . sprintf( __( 'Page %s', 'urbanmeandering' ), max( $paged, $page ) );
}


/*===========================*\
|															|
|					REQUIREMENTS				|
|															|
\*===========================*/

require_once( 'lib/um-customs.php' );
require_once( 'lib/um-metaboxes.php' );
require_once( 'lib/um-widgets.php' );

$args = array(
	'name'        	=> __('Sidebar-1'),
	'id'        	  => "sidebar-1",
	'class'		     	=> 'group',
	'before_widget' => '<section id="categories">',
	'after_widget'  => '</section>',
	'before_title'  => '<h3>',
	'after_title'   => '</h3>' );

	register_sidebar($args);

function um_contact_form($atts, $content=NULL){
    $str = '<div class="um_contact_form">';
    $str .= '<form method="POST" action="" id="contact_form">
             <ol>
						    <li><span> fields with * are required </span></li>
                <li>
                 <label for="email"> Email </label><input type="text" name="email" id="email" /> *
                 <div class="error_msg" id="email_msg"></div>
								</li>
                 <li>
                  <label for="fullname"> Fullname </label><input type="text" name="whole_name" id="fullname"/> *
									<div class="error_msg" id="fullname_msg"></div>
                 </li>
                 <li>
                  <label for="message"> Message </label><textarea name="comment" id="message"></textarea> *
									<div class="error_msg" id="message_msg"></div>
                 </li>
                <input type="submit" value="Submit" name="submit_question" />
             </ol>
            </form>';
		$str .= '<div id="message_sucess" > Your message has been sent. Thank you very much! </div>';
		$str .= '</div>';
    
    return $str;
}


if ( isset($_POST['submit_question']))
{
    global $wpdb;
    
    $comment = strip_tags(esc_attr($_POST['comment']));
    $whole_name = $_POST['whole_name'];
    $email = $_POST['email'];
    if ( !is_email($email) || strlen($comment) <= 0 || strlen($whole_name ) <= 0 )
    {
        
    }
    else
    {
			$data = array(	'whole_name' => $whole_name ,
											'email' => $email, 
											'comment' => $comment,
											'datecreated' => current_time('mysql'));
			$format = ('%s');
			$table = 'um_contact_form';
			
			$success = $wpdb->insert($table, $data, $format);
			
			//notify by email			
			wp_mail();
    }
}
add_shortcode('um_cform','um_contact_form');

function um_breadcrumbs($cat='')
{
	global $post; //echo '<pre>'; print_r($post); echo '</pre>';
	?>
	<div id="breadcrumbs">
		<ul> 
			<?php if ( is_page()): ?>
				<li><a href="<?php echo home_url(); ?>"> Home </a></li>
				<li><span>&nbsp;</span></li>
				<?php while( $post->post_parent != 0 ): ?>
				<?php $parent_post = get_post($post->post_parent); ?>
						<li><a href="<?php echo $parent_post->ID; ?>" ><?php  echo $parent_post->post_title; ?></a></li>
						<li><span>&nbsp;</span></li>
						<?php endwhile; ?>
						<li><?php echo $post->post_title; ?></li>
			<?php else: ?>
						<li><a href="<?php echo home_url(); ?>"> Home </a></li>
						<li><span>&nbsp;</span></li>
			<?php endif; ?>
						
			<?php if ( is_single()) : ?>
			<?php 
				$title = $post->post_type;
				switch( $post->post_type )
				{
					case 'travel-activity' : $title = 'Travels and Activities';break;
					case 'advocacy-charity' : $title = 'Advocacy and Charities';break;
				}		
			
				if ( $title )
				{
					?> <li><a href="<?php echo get_post_type_archive_link( $post->post_type ); ?>"> <?php echo ucfirst($title); ?></a></li> 
						 <li><span>&nbsp;</span></li> 
					<?php  
				}
			?>
			<li><?php echo $post->post_title; ?></li>
			<?php   endif; ?>
			
			<?php if ( is_archive() || is_search() || is_404() ): ?>
			<?php 
			 
			if ( $cat )
			{
				?> <li> <?php echo $cat; ?></li> 
					 
				<?php  
			}
			?>
			<?php endif; ?>
				
		</ul>
	</div>
<?php
}

function um_enqueue_scripts()
{
	if ( is_home()):
		wp_enqueue_style('flexslider-css', CSSDIR. 'flexslider.css');
		wp_enqueue_script('jquery-flexslider', JSDIR . 'jquery.flexslider-min.js', array('jquery'));
		wp_enqueue_script('jquery-easing', JSDIR .'jquery.easing.js');
		wp_enqueue_script('jquery-mousewheel', JSDIR . 'jquery.mousewheel.js');
		wp_enqueue_script('um_carousel', JSDIR. 'home_page.js');
	endif;
    
	if ( is_page(CONTACT_FORM))
	{
		wp_enqueue_script('contact_form',JSDIR .'contact_form.js',array('jquery'));
		$l10n = array('url' => home_url());
		wp_localize_script('contact_form', 'cform_obj', $l10n);
	}
}
add_action('wp_head','um_enqueue_scripts');


function um_admin_enqueue_scripts()
{
		wp_enqueue_script( 'jquery' );
		wp_enqueue_script('media-upload'); 
		wp_enqueue_script('thickbox'); 
		wp_enqueue_script('um-admin-js', JSDIR . 'um-admin.js', array('jquery', 'media-upload', 'thickbox'), '', '1.0');
		
		wp_enqueue_style('thickbox');
		wp_enqueue_style('um-admin-css', CSSDIR . 'um-admin.css', '','', '1.14');
}
add_action( 'init', 'um_admin_enqueue_scripts', 0 );

function urbanmeandering_comment( $comment, $args, $depth ) 
{
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) 
	{
		case 'pingback' :
		case 'trackback' :
			?>
				<li class="post pingback">
					<p><?php _e( 'Pingback:', 'urbanmeandering' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'urbanmeandering' ), '<span class="edit-link">', '</span>' ); ?></p>
			<?php
			break;
		default :
			?>
				<li <?php comment_class( 'group' ); ?> id="li-comment-<?php comment_ID(); ?>">
					<article id="comment-<?php comment_ID(); ?>" class="comment group">
						<footer class="comment-meta group">
							<div class="comment-author vcard group">
								<?php
								$avatar_size = 68;

								echo get_avatar( $comment, $avatar_size );
								printf( __( '%1$s %2$s', 'urbanmeandering' ),
									sprintf( '<span class="fn">%s</span>', get_comment_author_link() ),
									sprintf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>',
										esc_url( get_comment_link( $comment->comment_ID ) ),
										get_comment_time( 'c' ),
										sprintf( __( '%1$s at %2$s', 'urbanmeandering' ), get_comment_date(), get_comment_time() )
									)
								);
								?>

								<?php edit_comment_link( __( 'Edit', 'urbanmeandering' ), '<span class="edit-link">', '</span>' ); ?>
							</div>
							<?php 
							if ( $comment->comment_approved == '0' ) 
							{ 
								?>
									<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'urbanmeandering' ); ?></em>
									<br />
								<?php
							}
							?>
						</footer>

						<div class="comment-content"><?php comment_text(); ?></div>
						<div class="reply">
							<?php comment_reply_link( array_merge( $args, array( 'reply_text' =>'Reply', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
						</div>
					</article>
			<?php
			break;
	}
}

register_nav_menu('footer_menu','Urban Meandering Footer');
register_nav_menu('category_links','Urban Meandering Category Footer');

function um_carousel()
{
	$banners = get_posts( array(
								'post_type' => 'img_banner',
								'numberposts' => 4,
								'meta_key' => '_um_show_img',
								'meta_value' => 'on'
							));
   
	if ( $banners ):
	?>
		<div id="carousel" class="flexslider">
			<ul class="slides">
			 <?php foreach ( $banners as $banner ): ?>
				<?php $url = get_post_meta($banner->ID,'_um_custom_url',true); ?>
				<?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($banner->ID), 'large'); ?>
				<?php $thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($banner->ID), '85x70'); ?>
					<li data-thumb="<?php echo $thumbnail_image_url[0]; ?>">
				<?php echo get_the_post_thumbnail($banner->ID,'585x250'); ?>
					<span class="flex-caption"><a href="<?php echo $url; ?>"><?php echo $banner->post_title; ?> &raquo;</a></span>
			</li>
			<?php endforeach; ?>
			</ul>
		</div>
		<div class="flex-controls">
		</div>
	<?php  
	else: ?>
		<div id="banner">
			<img src="<?php echo IMG , 'banner-b.png'; ?>" alt="#" />
		</div> 
	<?php
	endif;
}

function page_navigation(){
	global $wp_query, $wp_rewrite;
	
	//echo '<pre>', print_r( $wp_query ), '</pre>';
	
	$args = array();
  $pages = '';
           
  $max = $wp_query->max_num_pages; //echo 'max ' . $max;
  //print_r($wp_query);
  if (!$current = get_query_var('paged')) $current = 1;
  $args['base'] = str_replace(999999999, '%#%', get_pagenum_link(999999999));
  $args['total'] = $max;
  $args['current'] = $current;
 
  $total = 1;
  $args['mid_size'] = 3;
  $args['end_size'] = 1;
  $args['prev_text'] = '«';
  $args['next_text'] = '»';
  
  if ($max > 1)
	{	
		echo '<div class="pagination group"><div class="wp-pagenavi">';
		if ( $total == 1 ) 
			$pages = '<span class="pages">Page ' . $current . ' of ' . $max . '</span>';
		
		echo $pages . paginate_links($args);
		echo '</div></div>';
	}
}

function um_custom_pagination_links( $page_link, $active_page, $query='', $limit, $total_pages )
{
	?>
	<div class="pagination group">
		<div class="wp-pagenavi">
		<?php
		$page_num_count = 7;
		
		$items_lr = ceil( ( $page_num_count - 1 ) / 2 );
		$min_page = $active_page - $items_lr;
		$min_page = ( $min_page <= 0 )? 1 : $min_page;
				
		$max_page = $active_page + $items_lr;
		
		if( ( $active_page - $min_page ) < $items_lr )
			$max_page += ( $items_lr - ( $active_page - $min_page ) );
			
		$max_page = ( $max_page > $total_pages ) ? $total_pages : $max_page;
		
		if( ( $max_page - $active_page ) < $items_lr )
		{
			$min_page -= ( $items_lr - ( $max_page - $active_page ) );
			$min_page = ( $min_page <= 0 ) ? 1 : $min_page;
		}
		?>
		
		<span class="pages"><?php echo 'Page ' . $active_page . ' of ' . $total_pages; ?></span>
		
		<?php
		if( $active_page > 1)
		{
			?>
			<a class="prev page-numbers" href="<?php echo $page_link , $query, ($active_page - 1); ?>">&laquo;</a>
			
			<?php 
			if( $min_page > 1 )
			{ 
				?>
				<a class="page-numbers" href="<?php echo $page_link , $query , '1'; ?>">1</a>
				<span class="page-numbers dots">&hellip;</span>
				<?php
			}
		}
		
		for($i=$min_page; $i<=$max_page; $i++)
		{
			if( $i == $active_page )
			{
				?>
				<span class="page-numbers current"><?php echo $i; ?></span>
				<?php
			}
			else
			{
				?>
				<a class="page-numbers" href="<?php echo $page_link , $query , $i; ?>"><?php echo $i; ?></a>
				<?php
			}
		}
		
		if( $total_pages != $active_page && $total_pages > $max_page )
		{
			?>
			<span class="page-numbers dots">&hellip;</span>
			<a class="page-numbers" href="<?php echo $page_link , $query, $total_pages; ?>"><?php echo $total_pages; ?></a>
			<?php
		}
		
		if( $active_page < $total_pages)
		{
			?>
			<a class="next page-numbers" href="<?php echo $page_link, $query , ($active_page + 1); ?>">&raquo;</a>
			<?php
		}
	?>
		</div>
	</div>
	<?php
}


function search_page_navigation(  $total, $limit, $active_page ){
	$total_pages = ceil( $total / $limit );
	
	if( $total_pages > 1 )
	{	
		$page_link = site_url();
		$search_query = '/?s=' . get_search_query() . '&page=';	
		
		um_custom_pagination_links( $page_link, $active_page, $search_query, $limit, $total_pages );
	}
}

function um_post_type_labels( $post )
{
	if( $post->post_type == 'travel-activity' )
	{
		return 'travels';
	}
	elseif( $post->post_type == 'advocacy-charity' )
	{
		return 'advocacy';
	}
	else
	{
		return $post->post_type;
	}
}